package com.campus_information_portal.service.impl;

import com.campus_information_portal.dao.PartyMemberDao;
import com.campus_information_portal.entity.PartyMember;
import com.campus_information_portal.entity.PartyMemberPartyStudent;
import com.campus_information_portal.entity.PartyMemberTemp;
import com.campus_information_portal.service.PartyMemberService;
import com.campus_information_portal.util.Message;
import com.campus_information_portal.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 20155883 万珊
 * @description 党支部成员业务实现
 * @project_name PartyMemberImpl
 * @package com.campus_information_portal.service.impl
 * @create 2018-05-12 17:15
 **/
@Service
public class PartyMemberImpl implements PartyMemberService{
    @Autowired
    private PartyMemberDao partyMemberDao;

    /**
     * @param partyMemberPartyStudent
     * @Description 查询党支部成员信息(分支部、身份、年级)
     * @author 20155883 万珊
     * @methodName queryPartyMemberByConditions
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @Override
    public Message queryPartyMemberByConditions(PartyMemberPartyStudent partyMemberPartyStudent) {
        List<PartyMemberPartyStudent> partyMemberPartyStudentList=partyMemberDao.queryPartyMemberByConditions(partyMemberPartyStudent);
        if (partyMemberPartyStudentList.size()!=0){
            return Message.success().add("partyMemberPartyStudentList",partyMemberPartyStudentList);
        }
        return Message.fail().add("1102","查询失败");
    }

//    /**
//     * @param partyMember
//     * @Description 修改党支部成员信息
//     * @author 20155883 万珊
//     * @methodName updatePartyMember
//     * @returnType boolean
//     * @create 2018/4/26 8:45
//     **/
//    @Override
//    public boolean updatePartyMember(PartyMember partyMember) {
//        boolean flag=partyMemberDao.updatePartyMember(partyMember);
//        return flag;
//    }

//    /**
//     * @param partyMember
//     * @Description 修改学生表中政治身份类型
//     * @author 20155883 万珊
//     * @methodName updateStudentPoli
//     * @returnType boolean
//     * @create 2018/4/26 8:45
//     **/
//    @Override
//    public boolean updateStudentPoli(PartyMember partyMember) {
//        boolean flag=partyMemberDao.updateStudentPoli(partyMember);
//        return flag;
//    }

    /**
     * @param party_member_account
     * @Description 删除党员
     * @author 20155883 万珊
     * @methodName deletePartyMember
     * @returnType boolean
     * @create 2018/4/26 8:45
     **/
    @Override
    public boolean deletePartyMember(int party_member_account) {
        boolean flag=partyMemberDao.deletePartyMember(party_member_account);
        return flag;
    }

    /**
     * @param party_member_account
     * @Description 修改学生表中政治身份类型为0（即除去其政治身份）
     * @author 20155883 万珊
     * @methodName updateStudentPoliWithO
     * @returnType boolean
     * @create 2018/4/26 8:45
     **/
    @Override
    public boolean updateStudentPoliWithO(int party_member_account) {
        boolean flag=partyMemberDao.updateStudentPoliWithO(party_member_account);
        return flag;
    }

    /**
     * @param party_organization_num
     * @Description 党支部成员统计，通过党支部编号
     * @author 20155883 万珊
     * @methodName countMember
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @Override
    public Message countMember(int party_organization_num) {
        int countMember=partyMemberDao.countMember(party_organization_num);
        if(countMember!=0){
            return Message.success().add("countMember",countMember);
        }
        return Message.fail().add("1107","查询失败");
    }
    /**
     * @param party_organization_num
     * @Description 根据党支部账户（教师编号/学生编号）查询所在党组织编号
     * @author 20155883 万珊
     * @methodName findPONBypartyMemberAccount
     * @returnType int
     * @create 2018/4/26 8:45
     **/
    @Override
    public int findPONBypartyMemberAccount(int party_organization_num) {
        int partyOrganizationNum=partyMemberDao.findPONBypartyMemberAccount(party_organization_num);
        return partyOrganizationNum;
    }

    /**
     * @param party_member_account
     * @Description 根据党支部账户（教师编号/学生编号）查询所在党员信息
     * @author 20155883 万珊
     * @methodName queryPartyMemberByPMA
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @Override
    public Message queryPartyMemberByPMA(int party_member_account) {
        PartyMember partyMember=partyMemberDao.queryPartyMemberByPMA(party_member_account);
        if (partyMember!=null){
            return Message.success().add("partyMember",partyMember);
        }
        return Message.fail().add("1108","查询失败");
    }



//    /**
//     * @param party_member_account
//     * @Description 查找党员临时表是否有某党员的修改记录
//     * @author 20155883 万珊
//     * @methodName queryPartyMemberTemp
//     * @returnType boolean
//     * @create 2018/4/26 8:45
//     **/
//    @Override
//    public boolean queryPartyMemberTemp(int party_member_account) {
//        boolean flag=partyMemberDao.queryPartyMemberTemp(party_member_account);
//        System.out.println("tempflag="+flag);
//        return flag;
//    }


    /**
     * @param partyMemberTemp
     * @Description 保存
     * @author 20155883 万珊
     * @methodName savePartyMember
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @Override
    public Message savePartyMember(PartyMemberTemp partyMemberTemp) {
        boolean coverFlag;
        boolean savePartyMemberFlag;
        boolean savePartyMemberTempFlag;

        PartyMemberTemp temp=partyMemberDao.queryPartyMemberTemp(partyMemberTemp);
        System.out.println("temp="+temp);
        //如果临时表里有记录，则覆盖原保存的数据，否则增加一条临时表数据
        if(temp!=null){
            coverFlag=partyMemberDao.coverPartyMemberTemp(partyMemberTemp);
            savePartyMemberFlag=partyMemberDao.savePartyMember(partyMemberTemp);

            System.out.println("-----临时表有数据------");
            System.out.println("coverFlag="+coverFlag+"--savePartyMemberFlag="+savePartyMemberFlag);

            if ((coverFlag==true)&&(savePartyMemberFlag==true)){
                return Message.success();
            }

        }else {
            savePartyMemberTempFlag=partyMemberDao.savePartyMemberTemp(partyMemberTemp,(int)System.currentTimeMillis());
            System.out.println("savePartyMemberTempFlag="+savePartyMemberTempFlag);
            savePartyMemberFlag=partyMemberDao.savePartyMember(partyMemberTemp);
            System.out.println("--savePartyMemberFlag="+savePartyMemberFlag);
            System.out.println("-----临时表无数据------");
            System.out.println("savePartyMemberTempFlag="+savePartyMemberTempFlag+"--savePartyMemberFlag="+savePartyMemberFlag);

            if ((savePartyMemberTempFlag==true)&&(savePartyMemberFlag==true)){
                return Message.success();
            }
        }

        return Message.fail().add("1109","保存失败");
    }

    /**
     * @param partyMember
     * @Description 提交
     * @author 20155883 万珊
     * @methodName savePartyMember
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @Override
    public Message commitPartyMember(PartyMember partyMember) {
        boolean flag1=partyMemberDao.commitPartyMember(partyMember);
        boolean flag2=partyMemberDao.commitPartyMemberTemp(partyMember);
        boolean flag3=partyMemberDao.updateStudentPoli(partyMember);

        if ((flag1==true)&&(flag2==true)&&(flag3==true)){
            return Message.success();
        }
        return Message.fail().add("1110","提交失败");
    }

    /**
     * @param partyMember
     * @Description 回滚
     * @author 20155883 万珊
     * @methodName rollback
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @Override
    public Message rollback(PartyMember partyMember) {
        boolean flag1=partyMemberDao.commitPartyMember(partyMember);
        boolean flag2=partyMemberDao.commitPartyMemberTemp(partyMember);

        if ((flag1==true)&&(flag2==true)){
            return Message.success();
        }
        return Message.fail().add("1111","回滚失败");
    }

    /**
     * @param party_member_account
     * @Description 查询该学生政治类型
     * @author 20155883 万珊
     * @methodName queryStudentPoli
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @Override
    public Message queryStudentPoli(int party_member_account) {
        int stuPoli=partyMemberDao.queryStudentPoli(party_member_account);
        return Message.success().add("stuPoli",stuPoli);
    }
}
